var moduleManageListUrl = basePath + "/module/manage/query";
var moduleManageDelUrl = basePath + "/module/manage/batchDel";
var moduleManageAddAndUpdateUrl = basePath + "/module/manage/addAndUpdate";

var dataGrid;
var schoolId = $("#userSchoolId").val();

$(function() {
	initSchoolIdValue();
	loadData();
});

function loadData() {
	dataGrid = $('#moduleManageDg').datagrid({
		url : moduleManageListUrl,
		toolbar: '#tb',
		fit : true,
		fitColumns : true,
		border : true,
		pagination : true,
		idField : 'id',
		pageSize : config.pageSize,
		autoRowHeight : 140,
		pageList : config.pageList,
		queryParams : {
			resourceRange: $("#resourceRange").val(),
			schoolId: schoolId
		},
		nowrap : true,
		striped : true,
		rownumbers : true,
		columns : [ [  {
			field : 'id',
			width : 10,
			align : "center",
			checkbox : true
		}, {
			field : 'ab',
			title : '模块id',
			resizable : true,
			align : "center",
			width : 20,
			formatter : function(value,row,index){
				return row.id;
			}
		}, {
			field : 'resourceName',
			title : '模块名称',
			width : 20,
			align : "center",
			editor:{type:'validatebox',options:{required:true}}
		}, {
			field : 'resourceUrl',
			title : '模块路径',
			width : 20,
			align : "center",
			editor:{type:'validatebox'}
		}, {
			field : 'resourceType',
			title : '模块类型',
			width : 10,
			align : "center",
			formatter : function(value,row,index){
				return row.resourceTypeValue;
			},
			editor:{
				type:'combobox',
				options:{
					valueField:'value',
					textField:'text',
					method:'get',
					url: basePath + "/common/resourceType",
					multiple:false,
					editable:false,
					required:true
				}
			}
		}, {
			field : 'icon',
			title : '图标',
			width : 10,
			align : "center",
			editor:{type:'validatebox'}
		}, {
			field : 'parentId',
			title : '父级id',
			width : 10,
			align : "center",
			editor:{type:'numberbox',options:{required:true}}
		}, {
			field : 'weight',
			title : '顺序',
			width : 10,
			align : "center",
			editor:{type:'numberbox',options:{required:true}}
		}, {
			field : 'isExtLink',
			align : 'center',
			title : '<b>是否外部链接</b>',
			width : 20,
        	formatter:function(value,row,index){
                if(value == 0){
                    return "否";
                }else{
                    return "是";
                }
            },
            editor:{
				type:'combobox',
				options: {  
                    data: [{"value": "N", "text": "否"},{"value": "Y", "text": "是"}],  
                    valueField: "value",  
                    textField: "text",  
                    editable: false,  
                    panelHeight:70,  
                    required: true  
                }  
			}
        }, {
			field : 'isDelete',
			align : 'center',
			title : '<b>是否有效</b>',
			width : 20,
        	formatter:function(value,row,index){
                if(value == 'N'){
                    return "有效";
                }else{
                    return "禁用";
                }
            },
            editor:{
				type:'combobox',
				options: {  
                    data: [{"value": "N", "text": "有效", selected: true},{"value": "Y", "text": "禁用"}],  
                    valueField: "value",  
                    textField: "text",  
                    selected: true, 
                    editable: false,  
                    panelHeight:70,  
                    required: true  
                }  
			}
        }, {
			field : 'ids',
			title : '职位',
			resizable : true,
			align : "center",
			width : 50,
			formatter : function(value,row,index){
				return row.idsValue;
			},
			editor:{
				type:'combobox',
				options:{
					valueField:'id',
					textField:'position',
					groupField:'depart',
					method:'get',
					url: basePath + "/organizeDepart/comboBox",
					multiple:true,
					editable:false
				}
			}
		}] ],
		onAfterEdit : function(rowIndex, rowData, changes){
			if(rowData.id){
				changes.id = rowData.id;
				changes.resourceRange = $("#resourceRange").val();
				changes.schoolId = schoolId
				addmodify(changes);
			}else {
				rowData.resourceRange = $("#resourceRange").val();
				rowData.schoolId = schoolId
				addmodify(rowData);
			}
		}
	});
}

function reload() {
	dataGrid.datagrid("clearSelections");
	searchForm("searchForm");
}

function searchForm(formId) {
	dataGrid.datagrid('load', $.serializeObject($('#' + formId)));
}
function resetForm(formId) {
	$("#" + formId)[0].reset();
}

function save(id) {
	if(dgEditIndex[id] != null){
		$('#' + id).datagrid('endEdit', dgEditIndex[id]);
		dgEdit[id] = null;
		dgEditIndex[id] = null;
	}
}

function addmodify(data) {
	$.ajax({
	   type: "POST",
	   url: moduleManageAddAndUpdateUrl,
	   data: data,
	   success: function(res){
		   if (res.code == '000'){
			   reload();
			   $.messager.alert('提示', data.id != null ? '变更成功' : '新增成功', 'info');
		   }else{
			   $.messager.alert('提示', data.id != null ? '变更失败' : '新增失败', 'error');
			   errorEdit("moduleManageDg");
		   }
		}
	});
}

function remove(id){
	if(dgEditIndex[id] != null){
		$.messager.alert('提示','请先保存或取消编辑！','info');
	}else{
		var dgObj = $('#' + id);
		var rows = dgObj.datagrid('getSelections');
		if(rows.length>0){
			$.messager.confirm("请确认","你确定要删除已选择的记录吗？",function(b){
				if(b){
					var delIds=[];
					for(var i = 0; i < rows.length; i++){
						delIds.push(rows[i].id);
					}
					$.ajax({
					   type: "POST",
					   url: moduleManageDelUrl,
					   data:{
						   ids: delIds
					   },
					   success: function(data){
						   dgEditIndex[id] = null;
						   if (data.code == '000'){
							   reload();
							   $.messager.alert('提示', '删除成功', 'success');
						   }else{
							   $.messager.alert('提示', '删除失败', 'success');
						   }
						}
					});
				}
			})
		}else{
			$.messager.alert('提示','请选择要删除的行！','error');
		}
	}
}
